package com.shenma2009.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenma2009.config.Config;
import com.shenma2009.domain.ResultResponse;
import com.shenma2009.mapper.TVehicleFaultInfoMapper;
import com.shenma2009.netty.client.WebSocketClient;
import com.shenma2009.pojo.ImitatePosition;
import com.shenma2009.pojo.TVehicleFaultInfo;
import com.shenma2009.service.ImitatePositionService;
import com.shenma2009.mapper.ImitatePositionMapper;
import com.shenma2009.thread.SendPositionThread;
import com.shenma2009.vo.VehicleDataVo;
import com.shenma2009.vo.VehicleFaultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
* @author 33509
* @description 针对表【imitate_position】的数据库操作Service实现
* @createDate 2023-05-29 11:31:37
*/
@Service
public class ImitatePositionServiceImpl extends ServiceImpl<ImitatePositionMapper, ImitatePosition>
    implements ImitatePositionService{

    @Autowired
    TVehicleFaultInfoMapper tVehicleFaultInfoMapper;

    @Autowired
    ImitatePositionMapper imitatePositionMapper;

    private static ExecutorService executor = Executors.newCachedThreadPool();

    /**
     * @description 发送故障报警
     * @author 军哥
     * @date 2023/5/29 15:29
     * @version 1.0
     */
    @Override
    public ResultResponse sendBaseFault(WebSocketClient webSocketClient, String vin) {

        try {
            TVehicleFaultInfo randomFault = tVehicleFaultInfoMapper.getRandomFault();

            VehicleFaultVo vehicleFaultVo = new VehicleFaultVo();
            BeanUtils.copyProperties(randomFault, vehicleFaultVo);
            vehicleFaultVo.setVin(vin);

            vehicleFaultVo.setLongitude("116.664053");
            vehicleFaultVo.setLatitude("39.531791");

            String vehicleMessage = JSON.toJSONString(vehicleFaultVo);

            System.out.println(vehicleMessage);

            String result = webSocketClient.pushMsg(Config.VEHICLE_BASE_FAULT + vehicleMessage);
            return ResultResponse.SUCCESS(result);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
        }

        return ResultResponse.SUCCESS();
    }

    /**
     * @description 发送实时轨迹
     * @author 军哥
     * @date 2023/5/29 15:29
     * @version 1.0
     */
    @Override
    public ResultResponse sendCurrentLocaltion(WebSocketClient webSocketClient, String vin) {

        ImitatePosition randomPositionPath = imitatePositionMapper.getRandomPositionPath();
        List<String> list = JSON.parseArray(randomPositionPath.getPosition(), String.class);

        SendPositionThread sendPositionThread = new SendPositionThread(list, webSocketClient, vin);
        Future<Boolean> submit = executor.submit(sendPositionThread);

        return ResultResponse.SUCCESS();
    }
}




